package util;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * @author wq3stone
 */
public class DBUtil {
    private static ResourceBundle bundle = ResourceBundle.getBundle("resources/db");
    private static String username = bundle.getString("username");
    private static String password = bundle.getString("password");
    private static String url = bundle.getString("url");
    private static String driver = bundle.getString("driver");
    private static Connection conn = null;

    public DBUtil() {
    }

    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        conn = DriverManager.getConnection(url, username, password);
        System.out.println("数据库已连接");
        return conn;
    }

    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("数据库已关闭");
    }
}
